package org.hp.Servlet;

import org.hp.service.RecordService;
import org.hp.service.RecordServiceImpl;
import org.hp.util.PageBean;
import org.hp.util.ResultJson;
import org.hp.util.ReturnJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/RecordServlet")
public class RecordServlet extends HttpServlet {
    RecordService recSer = new RecordServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        switch (method){
            case "QueryAll":
                QueryList(request,response);
                break;
            case "QueryHid":
                QueryHid(request,response);
                break;
            case "QueryLoid":
                QueryLoid(request,response);
                break;
            case "find":
                find(request,response);
                break;
            case "Delete":
                Delete(request,response);
                break;
            case "addJsp":
                addJsp(request,response);
                break;
            case "add":
                add(request,response);
                break;
            case "ShowOne":
                ShowOne(request,response);
                break;
            case "Update":
                    Update(request,response);
                break;

        }
    }

    private void QueryLoid(HttpServletRequest request, HttpServletResponse response) {
        List<Map> list = recSer.queryLoid();
//        System.out.println("select下拉列表loid"+list.toString());
        ReturnJsonData.returnJson(ResultJson.success(list),response);

    }

    private void QueryHid(HttpServletRequest request, HttpServletResponse response) {
        List<Map> list = recSer.queryHid();
//        System.out.println("select下拉列表hid"+list.toString());
        ReturnJsonData.returnJson(ResultJson.success(list),response);
    }

    private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map> list = recSer.queryAll();
        ReturnJsonData.returnJson(ResultJson.success(list),response);
//        request.getRequestDispatcher("/WEB-INF/jsp/record/showAll.jsp").forward(request,response);
    }

    private void QueryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page=Integer.valueOf(request.getParameter("page"))!=null?Integer.valueOf(request.getParameter("page")):1;;
        int limit=Integer.valueOf(request.getParameter("limit"))!=null?Integer.valueOf(request.getParameter("limit")):10;
        String reremark = request.getParameter("reremark")!=null?"%"+request.getParameter("reremark")+"%":"%%";
        String sql="select reid,h.hid,harea,hestate,hunitNumber,hroomNo,loname,reremark,retime from house h,logistics l,record r where " +
                "reremark like '" +reremark + "' and h.hid = r.hid and r.loid = l.loid";
        PageBean<Map> pageList = recSer.queryList(page,limit,sql,reremark);
        System.out.println(pageList);
        ReturnJsonData.returnJson(ResultJson.success(pageList),response);
//        request.getRequestDispatcher("/WEB-INF/jsp/record/showAll.jsp").forward(request,response);
    }

    private void Update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        System.out.println("修改活动信息servlet");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int reid=Integer.valueOf(request.getParameter("reid"));
        int hid=Integer.valueOf(request.getParameter("hid"));
        int loid=Integer.valueOf(request.getParameter("loid"));
        String reremark = request.getParameter("reremark");
//        String time = request.getParameter("time");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date retime = sdf.parse(time);
//        System.out.println("record修改Servlet："+hid+loid+ retime+ reremark+reid);
        int num = recSer.update(hid, loid, reremark,reid);
        if (num > 0){
//            System.out.println("修改成功");
            ReturnJsonData.returnJson(ResultJson.success(),response);
        }else{
//            System.out.println("修改失败");
            ReturnJsonData.returnJson(ResultJson.error(),response);
        }
    }

    private void ShowOne(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int reid=Integer.valueOf(request.getParameter("reid"));
        Map<String, Object> map = recSer.queryOne(reid);
        request.setAttribute("map",map);
        ReturnJsonData.returnJson(ResultJson.success(map),response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int hid=Integer.valueOf(request.getParameter("hid"));
        int loid=Integer.parseInt(request.getParameter("loid"));
        String time = request.getParameter("time");
        String reremark = request.getParameter("reremark");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println("时间（String格式）"+time);
        Date retime = null;
        try {
            retime = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        System.out.println("时间（date类型）"+retime);
        int num = recSer.add(hid, loid, retime, reremark);
        if (num > 0){
//            System.out.println("添加成功");
            ReturnJsonData.returnJson(ResultJson.success(),response);
        }else{
//            System.out.println("添加失败");
            ReturnJsonData.returnJson(ResultJson.error(),response);
//            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    private void addJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("record跳转新增页面");
        request.getRequestDispatcher("/view/record/add.jsp").forward(request,response);
    }

    private void Delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int reid=Integer.valueOf(request.getParameter("reid"));
            int num = recSer.delete(reid);
            if (num > 0){
//                System.out.println("删除成功");
                ReturnJsonData.returnJson(ResultJson.success(),response);
            }else{
//                System.out.println("删除失败");
                ReturnJsonData.returnJson(ResultJson.error(),response);
            }
    }

//    private void QueryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("修缮记录查所有：");
//        List<Map> list = recSer.queryAll();
//        //这里是传值给jsp的
//        /*request.setAttribute("list",list);
//        request.getRequestDispatcher("/WEB-INF/jsp/record/showAll.jsp").forward(request,response);*/
//        //code msg count data
//        ReturnJsonData.returnJson(ResultJson.success(list),response);
//    }
}
